<?php
/**
 * 首页
 * member: tsang
 * Date: 2018/3/14
 * Time: 16:16
 */

namespace app\api\logic;

use app\api\BaseModel;
use think\Db;

class Index extends BaseModel
{

    
    public function __construct(array $data)
    {
        parent::__construct($data);
    }


    /**
     * 首页推广
     * @author tsang
     */
    public function ad(){
        $list = db('ad')->alias('a')
            ->join('goods_brand gb','a.brand_id = gb.id')
            ->field('a.id,a.title,a.subtitle,a.content,a.type,a.brand_id,image,gb.name as brand_name')
            ->where(['a.is_show' => 1])
            ->order('a.sort desc')
            ->select();
        if(!empty($list)){
            $m_goods = db('goods_option_value');
            foreach ($list as &$l){
                $l['image'] = IMG_PATH.str_replace('\\',"/",$l['image']);
                if($l['type'] == '1'){
                    $goods = $m_goods->where(['goodsid' => $l['content']])->order('price asc')->find();
                    if($goods){
                        $l['goods_price'] = $goods['price'];
                    }
                }
            }
        }
        return $list;
    }

    /**
     * 推广文章
     * @author tsang
     */
    public function ad_detail(){
        $data = db('ad')->field('id,content')->where(['id' =>$this->requestData['id'],'type' => 2,'is_show' => 1])->find();
        if(!$data) return array('message' => '找不到');
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $data;
    }

    /**
     * 轮播图
     * @author tsang
     */
    public function banner(){
        $data = db('banner')->field('id,pic,title,url')
            ->where(['status' => 1])
            ->order('sort desc')
            ->select();
        if(!empty($data)){
            foreach ($data as &$d){
                $d['pic'] = IMG_PATH.str_replace('\\',"/",$d['pic']);
            }
        }
        return $data;
    }


    
}
